package com.woniuxy.controller.UserRelated;


import com.woniuxy.demo.Result;
import com.woniuxy.dto.commonDto.BaseDto;
import com.woniuxy.dto.UserRelatedDto.UserDto;
import com.woniuxy.dto.UserRelatedDto.UserTableDto;
import com.woniuxy.enums.CodeEnum;
import com.woniuxy.exception.PawnEmPowerException;
import com.woniuxy.exception.PawnRoleException;
import com.woniuxy.exception.PawnUserException;
import com.woniuxy.param.*;
import com.woniuxy.param.UserRelatedParam.SaveUserParam;
import com.woniuxy.param.UserRelatedParam.SaveUserRoleParam;
import com.woniuxy.param.UserRelatedParam.UpdateUserParam;
import com.woniuxy.param.UserRelatedParam.UserPageAndByKeyParam;
import com.woniuxy.service.UserTableService;
import com.woniuxy.util.ValidatedUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zcx, ftl, gr, rhs
 * @since 2021-12-31
 */
@RestController
@RequestMapping("/user-table")
@Api(tags = "用户相关接口")
public class UserTableController {
    @Resource
    private UserTableService uts;

    /**
     * 登录失败
     *
     * @return
     */
    @ApiOperation("登录失败")
    @GetMapping("/noneLogin.do")
    public Result noneLogin() {
        return Result.newInstance().setCode(CodeEnum.UNLOGIN_FAIL.getCode()).setMsg(CodeEnum.UNLOGIN_FAIL.getMessage());
    }


    /**
     * 用户自己修改自己的数据
     *
     * @param ump
     * @param result
     * @return
     */
    @ApiOperation("用户自己修改自己的数据")
    @PostMapping("/updateByMyself.do")
    public Result updateByMyself(@RequestBody @Validated UpdateMyselfParam ump, BindingResult result) {
        //参数验证
        ValidatedUtil.ValidatedException(result);

        boolean b = uts.UpdateMyselfParam(ump);
        if (b) {
            return Result.newInstance().setCode(200).setMsg("修改成功");
        }
        throw new PawnRoleException(CodeEnum.USER_UPDATE_FAIL.getMessage(), CodeEnum.USER_UPDATE_FAIL.getCode());
    }


    /**
     * 根据id查询用户（因为存入了redis,做人员增删改的时候要修改）
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @ApiOperation("根据id查询用户")
    @GetMapping("/findUserById/{userId}")
    @ApiImplicitParam(name = "userId", value = "用户id")
    public Result findUserById(@PathVariable Integer userId) throws Exception {

        UserTableDto userTableDto = uts.findUserById(userId);

        return Result.newInstance().setMsg("查询成功").setCode(200).setData(userTableDto);
    }

    /**
     * 修改用户数据
     *
     * @param uup
     * @param result
     * @return
     */
    @ApiOperation("修改用户数据")
    @PostMapping("/updateUser.do")
    public Result updateUser(@RequestBody @Validated UpdateUserParam uup, BindingResult result) {
        //参数验证
        ValidatedUtil.ValidatedException(result);

        boolean b = uts.updateUser(uup);
        if (b) {
            return Result.newInstance().setCode(200).setMsg("修改成功");
        }

        throw new PawnUserException(CodeEnum.ROLE_UPDATE_FAIL.getMessage(), CodeEnum.ROLE_UPDATE_FAIL.getCode());

    }


    /**
     * 添加新用户
     *
     * @param sup
     * @return
     */
    @ApiOperation("添加新用户")
    @PostMapping("/SaveUser.do")
    public Result SaveUser(@RequestBody @Validated SaveUserParam sup, BindingResult result) {
        //参数验证
        ValidatedUtil.ValidatedException(result);
        UserDto dto = uts.SaveUser(sup);
        if (!StringUtils.isEmpty(dto)) {
            return Result.newInstance().setCode(200).setMsg("添加成功").setData(dto);
        }
        throw new PawnUserException(CodeEnum.USER_SAVE_FAIL.getMessage(), CodeEnum.USER_SAVE_FAIL.getCode());
    }


    /**
     * 查询所有用户
     *
     * @param upp
     * @return
     */
    @ApiOperation("查询所有用户")
    @PostMapping("/listUsers.do")
    public Result listUsers(@RequestBody UserPageAndByKeyParam upp) {

        BaseDto<UserTableDto> pad = uts.listUsers(upp);

        return Result.newInstance().setCode(200).setMsg("查询成功").setData(pad);
    }

    /**
     * 修改用户绑定的角色授权
     *
     * @param arp
     * @return
     */
    @ApiOperation("修改用户绑定的角色授权")
    @PostMapping("/saveUserRole.do")
    public Result saveUserRole(@RequestBody SaveUserRoleParam arp) {
        if (uts.saveUserRole(arp)) {
            return Result.newInstance().setCode(200).setMsg("授权成功");
        }
        throw new PawnEmPowerException(CodeEnum.EMPOWER_FAIL.getMessage(), CodeEnum.CODE_ERROR_PHONE.getCode());

    }


    /**
     * 批量删除用户(逻辑删除)
     *
     * @param ids
     * @return
     */
    @ApiOperation("批量删除用户(逻辑删除)")
    @PostMapping("/removeUserByIds.do")
    public Result removeUserByIds(@RequestBody List<Integer> ids) {
        if (uts.removeUserByIds(ids)) {
            return Result.newInstance().setCode(200).setMsg("删除成功");
        }

        throw new PawnUserException(CodeEnum.ROLE_DELETE_FAIL.getMessage(), CodeEnum.ROLE_DELETE_FAIL.getCode());

    }
}

